﻿namespace bitzhuwei.JsonFormat.TestConsole {
    internal class Program {
        static void Main(string[] args) {
            Console.WriteLine("Hello, JsonFormat Console!");
            var compiler = new bitzhuwei.JsonFormat.CompilerJson();
            var searchPattern = "*.json";
            var sourceFiles = Directory.GetFiles(@".\..\..\..\Jsons", searchPattern, SearchOption.AllDirectories);
            foreach (var fileName in sourceFiles) {
                Console.WriteLine($"Processing {fileName}");
                var sourceCode = File.ReadAllText(fileName);
                var tokens = compiler.Analyze(sourceCode);
                if (tokens.Error) { throw new Exception(tokens.ToString()); }
                using (var writer = new StreamWriter($"{fileName}.tokens")) {
                    tokens.Print(writer
#if DEBUG
                        , CompilerJson.stArray
#endif
                        );
                }
                var syntaxTree = compiler.Parse(tokens);
                if (syntaxTree.root == null) { throw new Exception(syntaxTree.ToString()); }
                using (var writer = new StreamWriter($"{fileName}.tree")) {
                    syntaxTree.root.Print(writer
#if DEBUG
                        , CompilerJson.stArray
#endif
                        );
                }
                var json = compiler.Extract(syntaxTree, tokens, sourceCode);
                ArgumentNullException.ThrowIfNull(json);
            }

            Console.WriteLine("done.");
        }
    }
}
